
#include "managment.h"
#include "IAP_DATAFLASH.h"
#include "key.h"
#include "RF.h"


uchar ir_date[4];
u8 Rf_ID[4];
uint duibi;
uchar key,tim,lianfa,RF_BUFF,c_byte,c_bit,kadd,kpush,setjs,learn_t;
BitStatus  lf,lingma,tongbu,yj,cont_Addr_N;
void key_chuli();
void hualun();
//----------------------1---2---3---4---5--|-6---7---8---9---10|-------------|--	   |		 |		   |		 |		   |										|
uchar code dim_r[128]={255,255,255,255,255, 255,255,255,255,255, 200,180,160,140,120, //15
						100,80,70,60,50, 40,35,30,25,24, 20,18,16,14,12, 10,0,0,0,0,	//35
						0,0,0,0,0, 0,0,0,0,0,	//45
						0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, //	60
						0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 8,9,11,13,15, //85
						17,20,24,30,40, 50,60,70,80,90, 100,120,140,160,180, 200,255, //100
						255,255,255,
						255,255,255,255,255, 255,255,255,255,255, 255,255,255,255,255,	//120
						255,255,255,255,255, 255,255,255};

uchar code dim_g[128]={0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,
					   0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, //	30
					   0,0,0,8,10,12,14,16,18,19, 20,25,30,40,50,
					   65,80,100,130,160, 200,255,255,255,255, 255,255,255,255,255, //	60
					   255,255,255,255,255, 255,255,255,255,255, 255,255,255,255,255,
					   255,255,255,255,255, 255,255,255,255,255, 255,255,255,255,255,// 90
					   255,255,255,255,255, 255,255,255,255,255, 255,200,160,120,100,
					   80,60,40,30,20, 15,11,9,0,0, 0,0,0,0,0,  //120
					   0,0,0,0,0, 0,0,0};

uchar code dim_b[128]={30,40,60,80,100, 130,160,200,255,255, 255,255,255,255,255,
					   255,255,255,255,255, 255,255,255,255,255, 255,255,255,255,255,//30
					   255,255,255,255,255, 255,255,255,255,255, 255,255,255,255,255,
					   255,255,255,255,255, 255,255,200,180,160, 140,120,100,90,80, //60
					   70,60,50,42,35, 30,26,23,22,21, 19,8,18,14,12,
					   0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,	 //90
					   0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 
					   0,0,0,0,0, 0,0,0,0,0, 0,0,0,8,9,	//120
					   10,11,13,15,16, 17,20,24};
//--------------------------------------------------

void rf_chuli()
{

     if(duibi>130)      //????????10ms
	  {
       for(c_byte=0;c_byte<4;c_byte++)  //4???
       {
            for(c_bit=0;c_bit<8;c_bit++)  //????8?
           {
							duibi=0;
							ir_date[c_byte]=ir_date[c_byte]<<1;  //????
							while(!rf)
							{
								if(duibi>40)
								{
									goto fail;
								}
							}   //??????????
							duibi=0;
							while(rf)
							{
								if(duibi>40)
								goto fail;
							}
							if(duibi>22)
							{
								ir_date[c_byte]=ir_date[c_byte]|0x01;  //1?
							}
							else if(duibi<8)
						 {
								goto fail;
						 }
           }
           c_bit=0;  //?????????
       }
			c_byte=0;//3???????
				if(ir_date[2]+ir_date[3]==255)
			{
						if(dizhi[0]==ir_date[0]&&dizhi[1]==ir_date[1])
					 {
								if(lingma==1)
							{
								lingma=0;
							}
								else
							{
								key_chuli();
							}
					}
					 else 
					{
						if(kadd==ir_date[0]&&kpush==ir_date[1])
						{
							 dizhi[0]=kadd;
							 dizhi[1]=kpush;
						}
						else
						{
							 kadd=ir_date[0];
							 kpush=ir_date[1];
						}
					}
			}
		tim=0;	
    }
fail:
		  nop; 
}

void key_chuli()
{
		  tim=0;      //??????????
			flag_dz=0;
		  if(ir_date[2]&0x80)
		  key=ir_date[2];         //????
		  else if(ir_date[2]>0)
		 {
				hualun();
		 }
	//--------------------------------------------------
			if(key==0x85||key==0x86||key==0x87)    //????????
		 {
			 if(yj)key=0;  //????????
			 else
			 {
				 yj=1;   //?????KEY?????????????,???????
			 }
		 }	  
}
//---------------------------------------------------------
void hualun()
{
	 
		if(led_buf[0]==0&&onoff)
	 {
			 led_buf[0]=0;
			 yk_r=dim_r[ir_date[2]];
			 yk_g=dim_g[ir_date[2]];
			 yk_b=dim_b[ir_date[2]];
			 temp_r=yk_r;
			 temp_g=yk_g;
			 temp_b=yk_b;
			 ledtime=0;
			 stor_time=0;
			 flag_stor=1;
	 } 
}